package com.hyfd.service.mp;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.hyfd.common.BaseJson;
import com.hyfd.common.GlobalSetHyfd;
import com.hyfd.common.Page;
import com.hyfd.dao.mp.AgentBillDiscountDao;
import com.hyfd.dao.mp.BillPkgDao;
import com.hyfd.dao.mp.PhoneSectionDao;
import com.hyfd.service.BaseService;

@Service
@Transactional

public class AgentBillDiscountSer extends BaseService{
	
	public Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private AgentBillDiscountDao agentBillDiscountDao;
	
	@Autowired
	BillPkgDao billPkgDao;
	
	@Autowired
	PhoneSectionDao phoneSectionDao;
	/**
	 * 根据主键获取记录
	 * @param id
	 * @return
	 */
	public Map<String, Object> getagentBillDiscountById(String id) {
		Map<String, Object> m=new HashMap<String, Object>();
		try{
			m=agentBillDiscountDao.selectByPrimaryKey(id);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return m;
	}
	
	/**
	 * 带着代理商Id跳转到代理商话费折扣页
	 * @param id
	 * @return
	 */
	public String agentBillDiscountListPage(String id){
		try{
			Session session=getSession();
			session.setAttribute("agentId", id);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return "mp/agentBillDiscountList";
	}
	
	
	/**
	 * 获取记录数量
	 * @param m
	 * @return
	 */
	public int getagentBillDiscountCount(Map<String, Object> m){
		int agentBillDiscountCount=0;
		try{
			agentBillDiscountCount=agentBillDiscountDao.selectCount(m);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return agentBillDiscountCount;
	}
	
	/**
	 * 根据手机号查询号段、话费包信息
	 * */
	public String sectionAndBillPkgGet(HttpServletRequest req){
		String str=null;
		try {
			Map<String, Object> m = getMaps(req);
			String phone = (String) m.get("phone");
			//获取手机号相关信息
			String section = (phone.length()==13)?phone.substring(0,5):phone.substring(0,7);//获取号段	
			Map<String, Object> sectionMessage = phoneSectionDao.selectBySection(section);
			if(sectionMessage!=null){
				Map<String, Object> agent = agentGetByUserId();
				String agentName = (String) agent.get("name");
				sectionMessage.put("agentName", agentName);
				String providerId = (String) sectionMessage.get("provider_id");
				m.put("providerId", providerId);
				List<Map<String, Object>> billPkgList = billPkgDao.getBillPkgByProId(m);
				sectionMessage.put("billPkgMessage", billPkgList);
			}						
			str = BaseJson.mapToJson(sectionMessage);
		} catch (Exception e) {
			getMyLog(e,log);
		}
		return str;
	}
	
	/**
	 * 根据包、代理商、运营商查询折扣
	 * */
	public String agentBillDiscountGet(HttpServletRequest req){
		String str="flase";
		try {
			Map<String, Object> m = getMaps(req);
			Map<String, Object> agent = agentGetByUserId();
			String agentId = (String) agent.get("id");
			m.put("agentId", agentId);
			m.put("provinceCode", "");
			Map<String, Object> agentBillDiscount = agentBillDiscountDao.agentBillDiscountGet(m);
			if(agentBillDiscount!=null){
			str= agentBillDiscount.get("discount")+"";
			}			
		} catch (Exception e) {
			getMyLog(e,log);
		}
		return str;
	}
	
	/**
	 * 根据条件分页获取代理商话费折扣列表数据并生成json
	 * @param req
	 * @return
	 */
	public String agentBillDiscountList(HttpServletRequest req){
		StringBuilder sb=new StringBuilder();
		try{
		Map<String, Object> m=getMaps(req); //封装前台参数为map
		Page p=getPage(m);//提取分页参数
		int total=getagentBillDiscountCount(m);
		p.setCount(total);
		int pageNum=p.getCurrentPage();
		int pageSize=p.getPageSize();
		
		sb.append("{");
		sb.append(""+getKey("page")+":"+p.getCurrentPage()+",");
		sb.append(""+getKey("total")+":"+p.getNumCount()+",");
		sb.append(""+getKey("records")+":"+p.getCount()+",");
		sb.append(""+getKey("rows")+":"+"");
		
		PageHelper.startPage(pageNum, pageSize);//mybatis分页插件
		List<Map<String, Object>> dataList=agentBillDiscountDao.selectAll(m);
		String dataListJson=BaseJson.listToJson(dataList);
		sb.append(dataListJson);
		sb.append("}");
		}catch(Exception e){
			getMyLog(e,log);
		}
		
		return sb.toString();
	}
	
	/**
	 * @功能描述：	根据条件获取全部代理商话费折扣列表数据并生成json
	 *
	 * @作者：HYJ		@创建时间：2016年1月2日
	 * @param req
	 * @return
	 */
	public String agentBillDiscountAllList(HttpServletRequest req){
		String str = null;
		try{
			Map<String, Object> m=getMaps(req); //封装前台参数为map
			List<Map<String, Object>> dataList=agentBillDiscountDao.selectAll(m);
			str = BaseJson.listToJson(dataList);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return str;
	}
	
	/**
	 * 添加代理商话费折扣信息
	 * @param req
	 * @return
	 */
	public String agentBillDiscountAdd(HttpServletRequest req){
		Session session=getSession();
		Map<String, Object> map = getMaps(req);
		String agentId = (String) map.get("agentId");
		boolean flag=false;
		try{
			String name = (String) map.get("name");
			String providerId = (String) map.get("providerId");
			if(providerId!=null&&providerId!=""&&agentId!=null&&agentId!=""){
				map.remove("name");
				List<Map<String, Object>> allList = agentBillDiscountDao.selectAll(map);
				if(allList.size()>0){
					int i = agentBillDiscountDao.delByAgentAndProvider(map);
					if(i>0){
						map.remove("providerId");
						map.remove("agentId");
						flag = agentBillDiscount(name,agentId,providerId,map);
					}
				}else{
					map.remove("dataType");
					map.remove("providerId");
					map.remove("agentId");
					flag = agentBillDiscount(name,agentId,providerId,map);
				}
			}else{
				session.setAttribute(GlobalSetHyfd.backMsg, "参数不完整,添加失败");
			}
			session.setAttribute(GlobalSetHyfd.backMsg, flag?"添加成功":"添加失败!");
		} catch (Exception e) {
			session.setAttribute(GlobalSetHyfd.backMsg, flag?"添加成功":"添加失败!");
			getMyLog(e,log);
		}
		return "redirect:/agentBillDiscountListPage/"+agentId;		
	}
	
	public boolean agentBillDiscount(String name,String agentId,String providerId,Map<String,Object> map){
		boolean flag = false;
		try {
			Map<String, Object> userInfoMap=getUser(); //取到当前用户信息
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				  String provinceCode = entry.getKey();
				  String discount = (String) entry.getValue();
				  if(discount!=null&&discount!=""){
					  Map<String,Object> m = new HashMap<String, Object>();
					  m.put("name", name);
					  m.put("agentId", agentId);
					  m.put("providerId", providerId);
					  m.put("provinceCode", provinceCode);
					  m.put("discount", discount);
					  
//					  List<String> dataPkgByProId = billPkgDao.getPkgId(m);
//					  	if(dataPkgByProId.size()>0){
//					  		for(String dataPkgId:dataPkgByProId){
//					  				m.put("createUser", userInfoMap.get("suName"));//放入用户
//					  				m.put("dataPkgId", dataPkgId);
//					  				int i = agentDataDiscountDao.insertSelective(m);
//					  				if(i>0){
//					  					flag = true;
//					  				}
//					  			}
//					  		}
					  }
				  }
		} catch (Exception e) {
			flag = false;
			getMyLog(e,log);
		}
		
		return flag;
	}
	
//	/**
//	 * 修改代理商话费折扣信息
//	 * @param req
//	 * @param id
//	 * @return
//	 */
//	public String agentBillDiscountEdit(HttpServletRequest req){
//		String agentId =null;
//		try{
//		boolean flag=false;
//		Map<String, Object> myData=getMaps(req);
//		agentId = (String) myData.get("agentId");
//		Map<String, Object> userInfoMap=getUser(); //取到当前用户信息
//		myData.put("updateUser", userInfoMap.get("suName"));//放入用户
//		int rows=agentBillDiscountDao.updateByPrimaryKeySelective(myData);
//		if(rows>0){
//			flag=true;
//		}
//		Session session=getSession();
//		session.setAttribute(GlobalSetHyfd.backMsg, flag?"修改成功":"修改失败");
//		}catch(Exception e){
//			getMyLog(e,log);
//		}
//		return "agentBillDiscountListPage/"+agentId;	
//	}
	
	/**
	 * 跳转到代理商话费折扣编辑页
	 * @param id
	 * @return
	 */
	public String agentBillDiscountEditPage(String id){
		try{
			Map<String, Object> agentBillDiscount=getagentBillDiscountById(id);
			Session session=getSession();
			session.setAttribute("agentBillDiscount", agentBillDiscount);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return "mp/agentBillDiscountEdit";
	}
	
	/**
	 * 带着模板Id跳转到代理商话费折扣详情页
	 * @param id
	 * @return
	 */
	public String agentBillDiscountDetailPage(String id){
		try{
			Session session=getSession();
			session.setAttribute("modelId", id);
		}catch(Exception e){
			getMyLog(e,log);
		}
		return "mp/agentBillDiscountDetailList";
	}
	
	
	/**
	 * 删除代理商话费折扣信息
	 * @param id
	 * @return
	 */
	public String agentBillDiscountDel(String id){
		String agentId="";
		try{
		boolean flag=false;
		int rows=agentBillDiscountDao.deleteByPrimaryKey(id);
		if(rows>0){
			flag=true;
		}
		
		Session session=getSession();
		agentId = (String) session.getAttribute("agentId");
		session.setAttribute(GlobalSetHyfd.backMsg, flag?"删除成功":"删除失败");
		
		}catch(Exception e){
			getMyLog(e,log);
		}
		return "agentBillDiscountListPage/"+agentId;		
	}
	
}
